// data = [
//   ['columnTitle1', 'columnTitle2', 'columnTitle3'], [1, 2, 3], [4, 5, 6]
// ]
export const exportExcel = (heads, body) => {
  // 循环遍历，每行加入tr标签，每个单元格加td标签
  let str = `<thead><tr>${heads.map(cell => `<th>${cell}</th>`).join('')}</tr></thead>`
  str += `<tbody>${body.map(row => `<tr>${row.map(cell => `<td>${cell}</td>`).join('')}</tr>`).join('')}</tbody>`
  // Worksheet名
  let worksheet = 'Sheet1'
  let uri = 'data:application/vnd.ms-excel;base64,'
  // 下载的表格模板数据
  let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
    <x:Name>${worksheet}</x:Name>
    <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
    </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
    </head><body><table>${str}</table></body></html>`
  // 下载模板
  window.location.href = uri + base64(template)

  // 输出base64编码
  function base64(s) {
    return window.btoa(unescape(encodeURIComponent(s)))
  }
}
